<?php

namespace App\Http\Controllers\Admin\GameTool;

use App\Models\Enum\MailStatus;
use App\Models\Enum\RechargeStatus;
use Auth;
use Illuminate\Http\Request;

use App\Http\Controllers\Admin\BaseController;

use App\Services\GameTool\GameRechargeService;
use App\Services\Config\GameRechargeItemService;
use App\Http\Requests\Admin\GameTool\RechargeCreateRequest;
use App\Http\Requests\Admin\GameTool\RechargelBatchCreateRequest;

class RechargeController extends BaseController
{

    public function __construct(GameRechargeService $gameRechargeService, GameRechargeItemService $gameRechargeItemService)
    {
        $this->gameRechargeService = $gameRechargeService;
        $this->gameRechargeItemService = $gameRechargeItemService;
    }

    //邮件发送
    public function index(Request $request)
    {
        $reqPageSize = $request->get('page_size', 10);

        $recharges = $this->gameRechargeService->getMails($request);

        return view('game_tool.recharge.index', compact('recharges', 'reqPageSize'));
    }

    //邮件审核
    public function check(Request $request)
    {
        $reqPageSize = $request->get('page_size', 10);

        $list = $this->gameRechargeService->getCheckList($request);

        return view('game_tool.recharge.check', compact('list', 'reqPageSize'));
    }

    //添加邮件
    public function create()
    {
        $items = $this->gameRechargeItemService->getItems();

        return view('game_tool.recharge.create', compact('items'));
    }

    //保存邮件
    public function store(RechargeCreateRequest $request)
    {
        $this->gameRechargeService->create($request);

        flash('提交成功')->success()->important();

        return $this->redirect($request);
    }

    //申请者修改邮件状态
    public function status($status, $id)
    {
        $recharge = $this->gameRechargeService->gameRechargeRepository->getById($id);

        if (empty($recharge)) {
            flash('操作失败')->error()->important();

            return redirect()->back();
        }

        $recharge->update(['status' => $status]);

        flash('更新状态成功')->success()->important();

        return redirect()->back();
    }

    //审核者修改邮件状态
    public function checkStatus($status, $id)
    {
        $recharge = $this->gameRechargeService->gameRechargeRepository->getById($id);

        if (empty($recharge)) {
            flash('操作失败')->error()->important();

            return redirect()->back();
        }

        if ($status == RechargeStatus::AGREE) {
            if (!$this->gameRechargeService->requestGameApi($recharge)) {
                flash('操作失败')->error()->important();

                return redirect()->back();
            }
        }

        $id = Auth::id();
        $recharge->update(['status' => $status, 'approver_admin_id' => $id]);

        flash('更新状态成功')->success()->important();

        return redirect()->back();
    }

    //添加批量邮件
    public function batchCreate()
    {
        return view('game_tool.recharge.batchCreate');
    }

    //保存批量邮件
    public function batchStore(RechargelBatchCreateRequest $request)
    {
        $this->gameRechargeService->batchCreate($request);

        flash('提交成功')->success()->important();

        return $this->redirect($request);
    }
}
